package com.example.task1.ui.AllService.Gover;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.GovernmentDetailsBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class GovernmentAppealDetailActivity extends BaseActivity {

    private TextView tvTitle;
    private TextView tvTime;
    private TextView tvUndertaker;
    private TextView tvContent;
    private TextView tvDetailResult;
    private int id;
    private Button btnSubMit;
    private GovernmentDetailsBean.DataDTO rows;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_government_appeal_detail);
        id = getIntent().getIntExtra("id", 0);
        initView();
        initData();
        btnSubMit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(GovernmentAppealDetailActivity.this);
                EditText et_title = new EditText(GovernmentAppealDetailActivity.this);
                EditText et_content = new EditText(GovernmentAppealDetailActivity.this);
                LinearLayout linearLayout = new LinearLayout(GovernmentAppealDetailActivity.this);
                builder.setTitle("提交诉求");
                et_title.setHint("输入标题");
                et_content.setHint("输入内容");
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                linearLayout.addView(et_title);
                linearLayout.addView(et_content);
                builder.setView(linearLayout);
                builder.setNegativeButton("提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String title = et_title.getText().toString();
                        String content = et_content.getText().toString();
                        if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
                            MyApplication.show("标题或评论不能为空");
                        } else {
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("title", title);
                            map.put("content", content);
                            map.put("undertaker", rows.getUndertaker());
                            map.put("appealCategoryId", id);
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/gov-service-hotline/appeal", Api.getBody(map));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    MyApplication.show("提交成功");
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });
                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/gov-service-hotline/appeal/" + id);
        Api.send(call, GovernmentDetailsBean.class, new CallBack<GovernmentDetailsBean>() {
            @Override
            public void success(GovernmentDetailsBean data) {
                rows = data.getData();
                tvContent.setText(rows.getContent());
                tvDetailResult.setText(rows.getDetailResult());
                tvTime.setText(rows.getCreateTime());
                tvUndertaker.setText(rows.getUndertaker());
                tvTitle.setText(rows.getTitle());
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "诉求详情";
    }

    private void initView() {
        tvTitle = findViewById(R.id.tv_title);
        tvTime = findViewById(R.id.tv_time);
        tvUndertaker = findViewById(R.id.tv_undertaker);
        tvContent = findViewById(R.id.tv_content);
        tvDetailResult = findViewById(R.id.tv_detailResult);
        btnSubMit = findViewById(R.id.btn_subMit);
    }
}